home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mgr / src / bitmap.c < prev    next >
C/C++ Source or Header  |  1990-09-23  |  7KB  |  276 lines

  1. /*  SUN version of portable bit-blt code.  (S. A. Uhler) 
  2.  *  The data on each line is padded to (BITS+1) bits
  3.  */
  4.  
  5. #ifdef sun
  6. #  include <sys/ioctl.h>
  7. #  include <sun/fbio.h>
  8. #  include <sys/file.h>
  9. #  include <sys/mman.h>
  10. #endif
  11. #ifdef atarist
  12. #  include <linea.h>
  13. #  include <osbind.h>
  14. char *_scrnbase;
  15. int _scrwidth, _scrheight, _scrplanes;
  16. short *Ptsin, *Intin;
  17. short _ptsin[10], _intsin[10];
  18. #endif
  19.  
  20. #include <stdio.h>
  21. #include "bitmap.h"
  22.  
  23. #define dprintf    if(bit_debug)fprintf
  24. int bit_debug;
  25. static int _s_start;        /* for our "vfree" */
  26. static _s_len;
  27.  
  28. /* open the display; it looks like memory */
  29.  
  30. BITMAP *
  31. bit_open(name)
  32. char *name;            /* name of frame buffer */
  33. {
  34.    BITMAP *result = BIT_NULL;
  35. #ifdef sun
  36.    int fd;                    /* file descriptor of frame buffer */
  37.    register DATA *addr;    /* address of frame buffer */
  38.    struct fbtype buff;    /* frame buffer parameters */
  39.    int pagesize;    
  40.    char *malloc();
  41.  
  42.    /* open the SUN display */
  43.  
  44.    if ((fd = open(name, O_RDWR)) < 0)
  45.       return (BIT_NULL);
  46.  
  47.    /* get the frame buffer size */
  48.  
  49.    if (ioctl(fd, FBIOGTYPE, &buff) < 0)
  50.       return (BIT_NULL);
  51.  
  52.    /* malloc space for frame buffer */
  53.  
  54.    pagesize = getpagesize();
  55.    if ((_s_start = (int) malloc(buff.fb_size + pagesize)) == 0)
  56.       return (BIT_NULL);
  57.  
  58.    /* align space (and fb size) on a page boundary */
  59.  
  60.    buff.fb_size = (buff.fb_size+pagesize-1) &~ (pagesize-1);
  61.    addr = (DATA *) ((_s_start + pagesize - 1) & ~(pagesize - 1));
  62.  
  63.    /* map the frame buffer into malloc'd space */
  64.  
  65. #ifdef _MAP_NEW      /* New semantics for mmap in Sun release 4.0 */
  66.    addr = (DATA) mmap(addr, _s_len=buff.fb_size,
  67.                    PROT_READ|PROT_WRITE, _MAP_NEW|MAP_SHARED, fd, 0);
  68.    if ((int)addr == -1)
  69.       return (BIT_NULL);
  70. #else
  71.    if (mmap(addr, _s_len = buff.fb_size, PROT_WRITE, MAP_SHARED, fd, 0) < 0)
  72.       return (BIT_NULL);
  73. #endif
  74.  
  75.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  76.       return (BIT_NULL);
  77.  
  78.    result->primary = result;
  79.    result->data = addr;
  80.    result->x0 = 0,
  81.    result->y0 = 0,
  82.    result->wide = buff.fb_width;
  83.    result->high = buff.fb_height;
  84.    result->type = _SCREEN;
  85. #endif sun
  86. #ifdef atarist
  87.    extern void *malloc();
  88.  
  89.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  90.       return (BIT_NULL);
  91.  
  92.    result->primary = result;
  93.  
  94.    linea0();
  95.  
  96.    Intin = __aline->_INTIN;
  97.    if (!Intin) {
  98.     __aline->_INTIN = Intin = _intsin;
  99.    }
  100.    Ptsin = __aline->_PTSIN;
  101.    if (!Ptsin) {
  102.     __aline->_PTSIN = Ptsin = _ptsin;
  103.    }
  104.    _scrplanes = __aline->_VPLANES;
  105.    _scrheight = V_Y_MAX;
  106.    _scrwidth = V_X_MAX;
  107.  
  108.    result->data = Logbase();
  109.  
  110.    result->x0 = 0;
  111.    result->y0 = 0;
  112.    result->wide = _scrwidth;
  113.    result->high = _scrheight;
  114.    result->type = _SCREEN;
  115. #endif atarist   
  116.    return (result);
  117. }
  118.  
  119. /* destroy a bitmap, free up space */
  120.  
  121. int
  122. bit_destroy(bitmap)
  123. BITMAP *bitmap;
  124. {
  125.    if (bitmap == (BITMAP *) 0)
  126.       return (-1);
  127.  
  128.    if (IS_MEMORY(bitmap) && IS_PRIMARY(bitmap))
  129.       free(bitmap->data);
  130. #ifndef atarist
  131.    else if (IS_SCREEN(bitmap) && IS_PRIMARY(bitmap)) {
  132.       munmap(BIT_DATA(bitmap), _s_len);
  133.       free(_s_start);
  134.         }
  135. #endif
  136.    free(bitmap);
  137.    return (0);
  138. }
  139.  
  140. /* create a bitmap as a sub-rectangle of another bitmap */
  141.  
  142. BITMAP *
  143. bit_create(map, x, y, wide, high)
  144. BITMAP *map;
  145. int x, y, wide, high;
  146. {
  147.    char *malloc();
  148.    register BITMAP *result;
  149.  
  150.    if (x + wide > map->wide)
  151.       wide = map->wide - x;
  152.    if (y + high > map->high)
  153.       high = map->high - y;
  154.    if (wide < 1 || high < 1)
  155.       return (BIT_NULL);
  156.  
  157.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  158.       return (BIT_NULL);
  159.  
  160.    result->data = map->data;
  161.    result->x0 = map->x0 + x;
  162.    result->y0 = map->y0 + y;
  163.    result->wide = wide;
  164.    result->high = high;
  165.    result->primary = map->primary;
  166.    result->type = map->type;
  167.    return (result);
  168. }
  169.  
  170. /* allocate space for, and create a memory bitmap */
  171.  
  172. BITMAP *
  173. bit_alloc(wide, high, data, bits)
  174. unsigned short wide, high;
  175. DATA *data;
  176. int bits;    /* in preparation for color */
  177. {
  178.    char *malloc();
  179.    register BITMAP *result;
  180.    register int size;
  181.  
  182.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  183.       return (result);
  184.  
  185.    result->x0 = 0;
  186.    result->y0 = 0;
  187.    result->high = high;
  188.    result->wide = wide;
  189.  
  190.    size = ((wide+BITS)/8) * high;
  191.  
  192.    if (data != (DATA *) 0) {        /* point to user supplied data */
  193.       result->data = data;
  194.  
  195.         /* convert from external to internal format (if required) */
  196.  
  197.       if (DOFLIP) {
  198.          flip(data,size/sizeof(DATA));
  199.          }
  200.         }
  201.    else {                    /* malloc space: better be DATA aligned */
  202.         if ((result->data = (DATA *) malloc(size)) == (DATA *) 0) {
  203.           free(result);
  204.           return ((BITMAP *) 0);
  205.            }
  206.         bzero(result->data,size);
  207.         }
  208.  
  209. #ifdef atarist
  210.     if ((int) result->data & 1)
  211.       fprintf(stderr,"bit_alloc(): boundary alignment error\n");
  212. #else
  213.     if ((int) result->data &3)
  214.       fprintf(stderr,"bit_alloc(): boundary alignment error\n");
  215. #endif
  216.  
  217.    result->primary = result;
  218.    result->type = _MEMORY;
  219.    return (result);
  220.     }
  221.  
  222. /* flip the bit order on count elements of s */
  223.  
  224. static unsigned char flp[256] = {
  225.     0x00,    0x80,    0x40,    0xc0,    0x20,    0xa0,    0x60,    0xe0,
  226.     0x10,    0x90,    0x50,    0xd0,    0x30,    0xb0,    0x70,    0xf0,
  227.     0x08,    0x88,    0x48,    0xc8,    0x28,    0xa8,    0x68,    0xe8,
  228.     0x18,    0x98,    0x58,    0xd8,    0x38,    0xb8,    0x78,    0xf8,
  229.     0x04,    0x84,    0x44,    0xc4,    0x24,    0xa4,    0x64,    0xe4,
  230.     0x14,    0x94,    0x54,    0xd4,    0x34,    0xb4,    0x74,    0xf4,
  231.     0x0c,    0x8c,    0x4c,    0xcc,    0x2c,    0xac,    0x6c,    0xec,
  232.     0x1c,    0x9c,    0x5c,    0xdc,    0x3c,    0xbc,    0x7c,    0xfc,
  233.     0x02,    0x82,    0x42,    0xc2,    0x22,    0xa2,    0x62,    0xe2,
  234.     0x12,    0x92,    0x52,    0xd2,    0x32,    0xb2,    0x72,    0xf2,
  235.     0x0a,    0x8a,    0x4a,    0xca,    0x2a,    0xaa,    0x6a,    0xea,
  236.     0x1a,    0x9a,    0x5a,    0xda,    0x3a,    0xba,    0x7a,    0xfa,
  237.     0x06,    0x86,    0x46,    0xc6,    0x26,    0xa6,    0x66,    0xe6,
  238.     0x16,    0x96,    0x56,    0xd6,    0x36,    0xb6,    0x76,    0xf6,
  239.     0x0e,    0x8e,    0x4e,    0xce,    0x2e,    0xae,    0x6e,    0xee,
  240.     0x1e,    0x9e,    0x5e,    0xde,    0x3e,    0xbe,    0x7e,    0xfe,
  241.     0x01,    0x81,    0x41,    0xc1,    0x21,    0xa1,    0x61,    0xe1,
  242.     0x11,    0x91,    0x51,    0xd1,    0x31,    0xb1,    0x71,    0xf1,
  243.     0x09,    0x89,    0x49,    0xc9,    0x29,    0xa9,    0x69,    0xe9,
  244.     0x19,    0x99,    0x59,    0xd9,    0x39,    0xb9,    0x79,    0xf9,
  245.     0x05,    0x85,    0x45,    0xc5,    0x25,    0xa5,    0x65,    0xe5,
  246.     0x15,    0x95,    0x55,    0xd5,    0x35,    0xb5,    0x75,    0xf5,
  247.     0x0d,    0x8d,    0x4d,    0xcd,    0x2d,    0xad,    0x6d,    0xed,
  248.     0x1d,    0x9d,    0x5d,    0xdd,    0x3d,    0xbd,    0x7d,    0xfd,
  249.     0x03,    0x83,    0x43,    0xc3,    0x23,    0xa3,    0x63,    0xe3,
  250.     0x13,    0x93,    0x53,    0xd3,    0x33,    0xb3,    0x73,    0xf3,
  251.     0x0b,    0x8b,    0x4b,    0xcb,    0x2b,    0xab,    0x6b,    0xeb,
  252.     0x1b,    0x9b,    0x5b,    0xdb,    0x3b,    0xbb,    0x7b,    0xfb,
  253.     0x07,    0x87,    0x47,    0xc7,    0x27,    0xa7,    0x67,    0xe7,
  254.     0x17,    0x97,    0x57,    0xd7,    0x37,    0xb7,    0x77,    0xf7,
  255.     0x0f,    0x8f,    0x4f,    0xcf,    0x2f,    0xaf,    0x6f,    0xef,
  256.     0x1f,    0x9f,    0x5f,    0xdf,    0x3f,    0xbf,    0x7f,    0xff,
  257.     };
  258.  
  259. /*
  260.  * Convert from external to internal bitmap format.  Internal format
  261.  * is SUN-3 bit order, 1=black, 0=white.  This sample flip routine is
  262.  * for the DEC3100, where 1=white, 0=black, and the bits in every byte
  263.  * are revered
  264.  */
  265.  
  266. int
  267. flip(s,count,how)
  268. register DATA *s;
  269. register int count;
  270. int how;                        /* not used */
  271.     {
  272.     while (count-- > 0) 
  273.         *s++ = ~((flp[*s&0xff]) | (flp[*s>>8&0xff]<<8) |
  274.                  (flp[*s>>16&0xff]<<16) | (flp[*s>>24&0xff]<<24));
  275.     }
  276.